package com.example.wechat_b.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.widget.PopupMenu;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.example.wechat_b.R;
import com.example.wechat_b.activity.ChatActivity;
import com.example.wechat_b.bean.ChatBean;

import java.util.List;

/**
 * @author liaoqg
 * @brief description
 * @date 2021-11-01
 */
public class ChatListViewAdapter extends BaseAdapter {

    private Context context;
    private List<ChatBean> beanList;


    public ChatListViewAdapter(Context context,List<ChatBean> beans){
        this.context = context;
        this.beanList = beans;
    }

    @Override
    public int getCount() {
        return beanList.size();
    }

    @Override
    public Object getItem(int position) {
        return beanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.chat_list_item,parent,false);
            viewHolder = new ViewHolder();
            viewHolder.imageView = convertView.findViewById(R.id.chat_img);
            viewHolder.name = convertView.findViewById(R.id.chat_name);
            viewHolder.current = convertView.findViewById(R.id.chat_current);
            viewHolder.time = convertView.findViewById(R.id.chat_time);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        ChatBean bean = beanList.get(position);
        if (bean!=null){
            viewHolder.id = bean.getId();

//            viewHolder.imageView.setImageResource(bean.getImage());
            Glide.with(context)
                    .load(R.mipmap.xjp)
                    .transform(new RoundedCorners(5))
                    .into(viewHolder.imageView);

            viewHolder.name.setText(bean.getTitle());
            viewHolder.current.setText(bean.getCurrent());
            viewHolder.time.setText(bean.getTime());
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ChatActivity.class);
                intent.putExtra("name",bean.getTitle());
                context.startActivity(intent);
            }
        });
        convertView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                PopupMenu popupMenu = new PopupMenu(context,v);
                popupMenu.getMenuInflater().inflate(R.menu.fragment_list_poup,popupMenu.getMenu());
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        switch (item.getItemId()){
                            case R.id.item_delete:
                                break;
                            case R.id.item_toTop:
                                break;
                        }
                        return true;
                    }
                });
                popupMenu.setGravity(Gravity.CENTER);
                popupMenu.show();
                return true;
            }
        });
        return convertView;
    }

    class ViewHolder{
        public int id;
        public ImageView imageView;
        public TextView name,current,time;
    }
}
